package com.mini.repository;

import com.mini.model.BusinessTeacherInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author zhangxiaobin
 * @title: BusinessTeacherInfoRepository
 * @description: TODO
 * @date 2019/10/2715:58
 */
@Repository
public interface BusinessTeacherInfoRepository extends JpaRepository<BusinessTeacherInfo, Long>, JpaSpecificationExecutor<BusinessTeacherInfo> {

    List<BusinessTeacherInfo> findByGroupIdAndStatesOrderByOrdersAsc(Long groupId,Integer states);

    List<BusinessTeacherInfo> findByGroupIdIsInAndStatesOrderByOrders(Set<Long> groupIds,Integer states);

    BusinessTeacherInfo findByTeacherId(Long teacherId);

    List<BusinessTeacherInfo> findByDeptIdAndStates(Long deptId,Integer states);

    List<BusinessTeacherInfo> findByStates(Integer states);

    @Modifying
    @Query(value = "update business_teacher_info b set b.pb_states = ?2 where teacher_id = ?1 " , nativeQuery = true)
    void updateTeacherPbStates(@Param("teacherId") Long teacherId, @Param("useState") Integer useState);

    @Modifying
    @Query(value = "update business_teacher_info set pb_states = ?2 where teacher_id in (?1)" , nativeQuery = true)
    void batchUpdateTeacherPbStates(@Param("teacherIds") List<Long> teacherIds, @Param("useState") Integer useState);

    @Query(value = "SELECT t.group_id from business_teacher_info t where t.work_room_id =?1 group by t.group_id",nativeQuery = true)
    List<Long> findGroupIdsByDeptId(@Param("deptId")Long deptId);

    @Modifying
    @Query(value = "update business_teacher_info b set b.add_wechat_num = ?2 where b.teacher_id = ?1 " , nativeQuery = true)
    @Transactional
    void updateAddWechatNum(@Param("teacherId") Long teacherId, @Param("addWechatNum") Integer addWechatNum);

    List<BusinessTeacherInfo> findAllByTeacherIdIn(Set<Long> teacherIds);

}
